home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Programming / Programming Tools / Pascal Demos from Apple / window / WINDOW.TEXT next >
Encoding:
Text File  |  1985-04-05  |  13.5 KB  |  418 lines  |  [TEXT/ttxt]

  1. {$X-}   {Turn off stack expansion. This is a Lisa concept, not needed on Mac}
  2. {$U-}   {Turn off the Lisa Libraries. This is required by the WorkShop}
  3. {$R-}   {Turn off range checking}
  4. {$L-}
  5. Program Windows;
  6.  
  7. {  Jeffery J. Bradford  Macintosh Technical Support  April 1985  }
  8.  
  9. {This example will help you become familiar with how windows work}
  10. {in conjunction with the Event Manager. Recommened reading for   }
  11. {better understanding of this example is the Window Manager and  }
  12. {the Event Manager                                               }
  13.  
  14. { This particular example deals with windows and uses calls that }
  15. { that pertain only to windows and operations involving window   }
  16. { manipulation. It creates 2 windows by different methods. It    }
  17. { demonstrates how to activate, deactivate, grow, shrink, put-   }
  18. { away, bring back, and update a window.}
  19.  
  20.  
  21. USES
  22.     {$U Obj/Memtypes  } MemTypes,
  23.     {$U Obj/QuickDraw } QuickDraw,
  24.     {$U Obj/OSIntf    } OSIntf,
  25.     {$U Obj/ToolIntf  } ToolIntf,
  26.     {$U Obj/PackIntf  } PackIntf,
  27.     {$U Obj/MacPrint  } MacPrint;
  28.  
  29. CONST
  30. {menu stuff}
  31.     AppleMenu = 256;
  32.     FileMenu  = 257;
  33.     EditMenu  = 258;
  34.  
  35. {window IDs}
  36.     WindResID = 256;
  37.  
  38.  
  39. TYPE
  40. {this is useful stuff you might need sometime}
  41.  
  42.     WordStuff = Packed Record
  43.        Case Integer of
  44.          0: (word: Integer);
  45.          1: (chr1,chr0: Char);
  46.          2: (SByte1,SByte0: SignedByte);
  47.          3: (b15,b14,b13,b12,b11,b10,b9,b8,b7,b6,b5,b4,b3,b2,b1,b0: Boolean)
  48.        End;
  49.  
  50.     CharStuff = Packed Record
  51.          chr3,chr2,chr1,chr0: char;
  52.        End;
  53.  
  54.     LMWordPtr = ^Integer;          {pointer to low memory address}
  55.     LMLongPtr = ^LongInt;          {pointer to low memory address - long}
  56.  
  57.  
  58.  
  59. VAR
  60. {global program stuff}
  61.     Finished:      Boolean;    {used to terminate the program}
  62.     ClockCursor:   CursHandle; {handle to the waiting watch cursor}
  63.  
  64. {Screen stuff}
  65.     DragArea:   Rect;          {holds the area where window can be dragged in}
  66.     GrowArea:   Rect;          {holds the area to which a window's size can change}
  67.     Screen:     Rect;          {holds the screen dimensions}
  68.  
  69. {Window stuff}
  70.     OneWindow:   WindowPtr;    {pointer to the first window}
  71.     TwoWindow:   WindowPtr;    {pointer to the second window}
  72.  
  73. {-----------------------------------------------------------------------------
  74.                      end of global variable definition
  75. -----------------------------------------------------------------------------}
  76.  
  77.  PROCEDURE Update_Scroll_Bar_Areas(ofTheWindow:WindowPtr);
  78.  Var BarArea: Rect;
  79.  
  80.  Begin
  81.  {invalidate the right scroll bar area first}
  82.    BarArea := ofTheWindow^.portRect; {makes the BarArea have window coordinates}
  83.    BarArea.left := BarArea.right-16; {move left in so its only the Scroll Bar Area}
  84.    InvalRect(BarArea);  {invalidate this portion of the window}
  85.  
  86.  {invalidate the bottom scroll bar area next }
  87.    BarArea := ofTheWindow^.portRect; {reset BarArea to window coordinates}
  88.    BarArea.top := BarArea.bottom-16; {move top down so its only the botton bar}
  89.    InvalRect(BarArea);
  90.  
  91.    {NOTE: is your drawing is fast just Invalidate the entire PortRect}
  92.  End;
  93.  
  94. {-----------------------------------------------------------------------------}
  95.  
  96.  PROCEDURE ReSizeWindow(theWindow:WindowPtr; MouseLoc: Point);
  97.  
  98.  Var NewSize: LongInt;
  99.      Width:   Integer;
  100.      Height:  Integer;
  101.  
  102.  Begin
  103.    NewSize := GrowWindow(theWindow,      { grow this window}
  104.                          MouseLoc,       { mouse location  }
  105.                          GrowArea);      { limits of growth - global var}
  106.    If NewSize <> 0 then
  107.    begin
  108.      Height :=  HiWord(NewSize);         {high word of..}
  109.      Width  :=  LoWord(NewSize);         {low word of...}
  110.  
  111.      Update_Scroll_Bar_Areas(theWindow); {erase where the scroll bars WERE}
  112.  
  113.      If Height< 16 then Height := 16; {don't let the window close on itself}
  114.      If Width < 16 then Width  := 16;
  115.  
  116. {now set the new size}
  117.      SizeWindow(theWindow,   {resize this Window}
  118.                     Width,   {set the width}
  119.                    Height,   {set the height}
  120.                     TRUE);   {set the update flag}
  121.  
  122.      Update_Scroll_Bar_Areas(theWindow);  {redraw where the scroll bars ARE now }
  123.  
  124.    end; {if size of window was changed}
  125.  End;
  126.  
  127. {-------------------------------------------------------------------}
  128.  
  129. PROCEDURE ProcessMenu_in(CodeWord:longint);
  130. Var
  131.   Menu_No:    integer;        {menu number that was selected}
  132.   Item_No:    integer;        {item in menu that was selected}
  133.   NameHolder: Str255;         {name holder for desk accessory or font}
  134.   DNA:        integer;        {OpenDA will never return 0, so don't care}
  135.  
  136. Begin
  137.   If CodeWord <> 0 then {go ahead and process the command}
  138.   begin
  139.     Menu_No := HiWord(CodeWord);   {get the Hi word of...}
  140.     Item_no := LoWord(CodeWord);   {get the Lo word of...}
  141.  
  142.     Case Menu_No of
  143.  
  144.      AppleMenu: Begin
  145.                   GetItem(GetMHandle(AppleMenu), Item_No, NameHolder);
  146.                   DNA := OpenDeskAcc(NameHolder);
  147.                 End;
  148.  
  149.       FileMenu: Begin
  150.                   Case Item_No of
  151.                     1: Finished := True;          {quit}
  152.                   End;
  153.                 End;
  154.  
  155.       EditMenu: Begin
  156.                   If Not SystemEdit(Item_no - 1) {if not for a desk accessory}
  157.                   then
  158.                     Case Item_No of
  159.                       1: begin end;               {undo}
  160.                     { 2:                           line divider}
  161.                       3: begin end;               {cut}
  162.                       4: begin end;               {copy}
  163.                       5: begin end;               {paste}
  164.                       6: begin end;               {clear}
  165.                     End;
  166.                 End;
  167.  
  168.     End;{case of Menu_No}
  169.  
  170.     HiliteMenu(0);               {unhilite after processing menu}
  171.   end; {the If codeword <> 0}
  172. End; {of ProcessMenu_in procedure}
  173.  
  174.  
  175. {-------------------------------------------------------------------}
  176. {----- These are procedures called from the main event loop  -------}
  177.  
  178. PROCEDURE DealwthMouseDowns(Event:EventRecord);
  179. Var Location: integer;
  180.     WindowPointedTo: WindowPtr;
  181.     MouseLoc:Point;
  182.     WindoLoc:integer;
  183. Begin
  184.   MouseLoc := Event.Where;
  185.   WindoLoc := FindWindow(MouseLoc, WindowPointedTo);
  186.   Case WindoLoc of
  187.  
  188.      inMenuBar: ProcessMenu_in(MenuSelect(MouseLoc));
  189.  
  190.      inSysWindow: SystemClick(Event,WindowPointedTo);
  191.  
  192.      inContent: If WindowPointedTo <> FrontWindow
  193.                 then SelectWindow(WindowPointedTo)
  194.                 else begin {do something} end;
  195.  
  196.      inGrow:    If WindowPointedTo <> FrontWindow
  197.                 then SelectWindow(WindowPointedTo)
  198.                 else ReSizeWindow(WindowPointedTo,MouseLoc);
  199.  
  200.      inDrag   :DragWindow(WindowPointedTo,MouseLoc,DragArea);
  201.  
  202.      inGoAway :If TrackGoAway(WindowPointedTo,MouseLoc)
  203.                then DisposeWindow(WindowPointedTo); {since W mgr allocated space}
  204.  
  205.   End{ of case};
  206. End;
  207.  
  208. {-----------------------------------------------------------------------------}
  209.  
  210. PROCEDURE DealwthKeyDowns(Event:EventRecord);
  211. Var CharCode:  char;
  212. Begin
  213.  
  214.   CharCode:= CharStuff(Event.message).Chr0; {get low byte w/no processing}
  215.  
  216.   If BitAnd(Event.modifier,CmdKey) = CmdKey
  217.    then
  218.      begin  {key board command - probably a menu command}
  219.        ProcessMenu_in(MenuKey(CharCode));
  220.      end
  221.    else
  222.      begin  {regular keyboard entry}
  223.        {TEKey(CharCode,TextHandle); if we had a text edit record}
  224.        {Scrolltext}
  225.      end;
  226. End;
  227.  
  228. {-----------------------------------------------------------------------------}
  229.  
  230. PROCEDURE DealwthActivates(Event: EventRecord);
  231. Var TargetWindow:WindowPtr;
  232. Begin
  233.    TargetWindow := WindowPtr(Event.message);
  234.    DrawGrowIcon(TargetWindow);
  235.  
  236.    If Odd(Event.modifiers) {then the window is becoming active}
  237.    then
  238.      begin
  239.        SetPort(TargetWindow);
  240.        {and activate whatever else you need}
  241.        {the scroll bars}
  242.        {hilite selected text}
  243.      end
  244.    else
  245.      begin
  246.        {deactivate whatever you need}
  247.        {deactivate the scroll bars}
  248.        {UNhilite selected text}
  249.      end;
  250. End;
  251.  
  252. {-----------------------------------------------------------------------------}
  253.  
  254. PROCEDURE DealwthUpdates(Event:EventRecord);
  255. Var UpDateWindow,
  256.           TempPort: WindowPtr;
  257. Begin
  258.    UpDateWindow := WindowPtr(Event.message);
  259.    GetPort(TempPort);                {Save the current port}
  260.  
  261.    SetPort    (UpDateWindow);      {set the port to one in Evt.msg}
  262.    BeginUpDate(UpDateWindow);
  263.      EraseRect(UpDateWindow^.portRect);
  264. (*     or
  265.      EraseRect(UpDateWindow^.VisRgn^^.rgnBBox); *)
  266.      DrawGrowIcon(UpDateWindow);
  267.    EndUpDate  (UpDateWindow);
  268.  
  269.    SetPort    (TempPort);             {restore to the previous port}
  270. End;
  271.  
  272. {-----------------------------------------------------------------------------}
  273.  
  274. PROCEDURE MainEventLoop;
  275. Var Event:EventRecord;
  276.     ProcessIt: Boolean;
  277. Begin
  278.   Repeat
  279.     SystemTask;             {so you can support Desk Accessories}
  280.  
  281.     ProcessIt := GetNextEvent(EveryEvent,Event);
  282.     If ProcessIt{is true} then {we'll ProcessIt}
  283.           Case Event.what of
  284.  
  285.             mouseDown  : DealwthMouseDowns(Event);
  286.             KeyDown    : DealwthKeyDowns  (Event);
  287.             ActivateEvt: DealwthActivates (Event);
  288.             UpDateEvt  : DealwthUpdates   (Event);
  289.  
  290.           End;{of Case}
  291.   Until Finished; {terminate the program}
  292. End;
  293.  
  294. {-----------------------------------------------------------------------------}
  295.  
  296. PROCEDURE InitThings;
  297. Begin
  298.   InitGraf(@thePort);          {create a grafport for the screen}
  299.  
  300.   MoreMasters;                 {extra pointer blocks at the bottom of the heap}
  301.   MoreMasters;                 {this is 5 X 64 master pointers}
  302.   MoreMasters;
  303.   MoreMasters;
  304.   MoreMasters;
  305.  
  306. {get the cursors we use and lock them down - no clutter}
  307.   ClockCursor := GetCursor(watchCursor);
  308.   HLock(Handle(ClockCursor));
  309.  
  310. {show the watch while we wait for inits & setups to finish}
  311.   SetCursor(ClockCursor^^);
  312.  
  313. {init everything in case the app is the Startup App}
  314.   InitFonts;                     {startup the fonts manager}
  315.   InitWindows;                   {startup the window manager}
  316.   InitMenus;                     {startup the menu manager}
  317.   TEInit;                        {startup the text edit manager}
  318.   InitDialogs(Nil);              {startup the dialog manager}
  319.  
  320.   Finished := False;             {set program terminator to false}
  321.   FlushEvents(everyEvent,0);     {clear events from previous program}
  322. End;
  323.  
  324. {-----------------------------------------------------------------------------}
  325.  
  326. PROCEDURE SetupLimits;
  327. Begin
  328.   Screen := ScreenBits.Bounds;   {set the size of the screen}
  329.   SetRect(DragArea,Screen.left+4,Screen.top+24,Screen.right-4,Screen.bottom-4);
  330.   SetRect(GrowArea,Screen.left,Screen.top+24,Screen.right,Screen.bottom);
  331. End;
  332.  
  333. {-----------------------------------------------------------------------------}
  334.  
  335. PROCEDURE SetupMenus;
  336. Var
  337.   MenuTopic: MenuHandle;
  338. Begin
  339.   MenuTopic := GetMenu(AppleMenu);  {get the apple desk accessories menu}
  340.   AddResMenu(MenuTopic,'DRVR');     {adds all names into item list}
  341.   InsertMenu(MenuTopic,0);          {put in list held by menu manager}
  342.  
  343.   MenuTopic := GetMenu(FileMenu);   {always need this for Quiting}
  344.   InsertMenu(MenuTopic,0);
  345.  
  346.   MenuTopic := GetMenu(EditMenu);   {always need for editing Desk Accessories}
  347.   InsertMenu(MenuTopic,0);
  348.  
  349.   DrawMenuBar;           {all done so show the menu bar}
  350. End;
  351.  
  352. {-----------------------------------------------------------------------------}
  353.  
  354. PROCEDURE SetupWindows;
  355. Var  WRect:    Rect;
  356.      Typ_ofW1: integer;
  357.      Visible:  boolean;
  358.      GoAway:   boolean;
  359.      RefVal:   LongInt;
  360.  
  361. Begin
  362. { Create the first window using the NewWindow Command }
  363.  
  364.   SetRect(WRect,10,40,230,150);  {set the size of the window -global coordinates}
  365.   Typ_ofW1 := 16;                {set window type - rounded corner   }
  366.   Visible := true;               {set the window to visible in its plane}
  367.   GoAway := true;                {give the window a GoAway box }
  368.  
  369.   OneWindow := NewWindow(Nil,           { Window Mgr will allocate space in Heap}
  370.                          WRect,         { rectangle with windows size}
  371.                          'Window 1',    { the title of the window }
  372.                          Visible,       { set the window to visible in its plane}
  373.                          Typ_ofW1,      { Window definition ID}
  374.                          Nil,           { behind ptr: window is set to last}
  375.                          GoAway,        { draw a goaway region in title area }
  376.                          RefVal);       { 32-bit value that can be used by App}
  377.  
  378.  
  379. { Create the second window from information stored on the Resource file }
  380.  
  381.   TwoWindow := GetNewWindow(WindResID,      {Resource ID where Window info is }
  382.                             NIL,            {W Mgr allocates space for W. Record}
  383.                             POINTER(-1));   {set the window to be in front    }
  384.  
  385. {NOTE: NewWindow and GetNewWindow have initiated an ActivatEvt and an }
  386. {      UpDateEvt event. They are being queued up by the event manager.}
  387. {      Also, the window record is Non-relocatable, so put it on the stack}
  388. {      or create it so that it is low in the heap space, reduce fragmentation}
  389. End;
  390.  
  391. {-----------------------------------------------------------------------------}
  392.  
  393. PROCEDURE SetUpThings;
  394. Begin
  395.   SetupWindows;         {do first so its low in heap}
  396.   SetupMenus;
  397.   SetupLimits;
  398.  
  399.   InitCursor;           {ready to go, so show the Arrow cursor}
  400. End;
  401.  
  402. {-----------------------------------------------------------------------------}
  403.  
  404. PROCEDURE CloseThings;
  405. Begin
  406. {close files, if you changed sys resources, UNchange them here be carefull}
  407. {about changing sys things, remember the Switcher could be around}
  408. End;
  409.  
  410. {-----------------------------------------------------------------------------}
  411.  
  412. BEGIN
  413.   InitThings;
  414.   SetUpThings;
  415.   MainEventLoop;
  416.   CloseThings;
  417. END.
  418.